package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseSelectObject;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.PayMoneyLogQueryParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.widget.SingleLineView;
import java.util.ArrayList;
import java.util.Date;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/21 16:29
 */
public class PayMoneyLogQueryFragment extends BaseFragment implements View.OnClickListener {
  private static final int REQUEST_START_TIME = 1;
  private static final int REQUEST_END_TIME = 2;
  private static final int REQUEST_TYPE = 3;
  @BindView(R.id.sl_startTime) SingleLineView slStartTime;
  @BindView(R.id.sl_endTime) SingleLineView slEndTime;
  @BindView(R.id.sl_billType) SingleLineView slBillType;
  @BindView(R.id.sl_orderNo) SingleLineView slOrderNo;
  private PayMoneyLogQueryParams mParams;

  @Override public void initData() {
    super.initData();
    mParams = (PayMoneyLogQueryParams) mBaseParams;
    if (mParams == null) mParams = new PayMoneyLogQueryParams();
  }

  @Override public void initView(View view, Bundle savedInstanceState) {
    super.initView(view, savedInstanceState);
    setTitleCustom(R.string.query);
    slBillType.setVisibility(View.GONE);
    slStartTime.setOnClickContentListener(this);
    slEndTime.setOnClickContentListener(this);
    slBillType.setOnClickContentListener(this);

    if (mParams == null) return;
    slStartTime.setTextContent(mParams.getStartDate());
    slEndTime.setTextContent(mParams.getEndDate());
    slBillType.setTextContent(mParams.getQueryMap().get(mParams.getQueryType()).getName());
    slOrderNo.setTextContent(mParams.getOrderNo());
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_pay_money_log_query, container, false);
    ButterKnife.bind(this, view);
    return view;
  }

  @Override public void onClick(View v) {
    if (!(v.getTag() instanceof Integer)) return;
    int id = (int) v.getTag();
    switch (id) {
      case R.id.sl_startTime:
        selectDate(MyDateUtil.parse(slStartTime.getTextContent()), REQUEST_START_TIME);
        break;
      case R.id.sl_endTime:
        selectDate(MyDateUtil.parse(slEndTime.getTextContent()), REQUEST_END_TIME);
        break;
      case R.id.sl_billType:
        selectType();
        break;
      default:
        break;
    }
  }

  protected void selectDate(Date date, int requestCode) {
    DialogFragment dialog = BaseYMDTimeDialogFragment.newInstance(date);
    dialog.setTargetFragment(this, requestCode);
    dialog.show(getActivity().getSupportFragmentManager(), "");
  }

  private void selectType() {
    if (mParams == null) return;
    DialogFragment dialog =
        SimpleArrayDialogFragment.newInstance(new ArrayList<>(mParams.getQueryMap().values()));
    dialog.setTargetFragment(this, REQUEST_TYPE);
    dialog.show(mActivity.getSupportFragmentManager(), "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;
    switch (requestCode) {
      case REQUEST_START_TIME:
        Date date = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
        slStartTime.setTextContent(MyDateUtil.format(date, MyDateUtil.TYPE_DATE));

        break;
      case REQUEST_END_TIME:
        Date endDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
        slEndTime.setTextContent(MyDateUtil.format(endDate, MyDateUtil.TYPE_DATE));
        break;
      case REQUEST_TYPE:
        BaseSelectObject result =
            (BaseSelectObject) data.getSerializableExtra(SimpleArrayDialogFragment.EXTRA_VALUE);
        slBillType.setTextContent(result.getName());
        mParams.setQueryType(result.getCode());
      default:
        super.onActivityResult(requestCode, resultCode, data);
    }
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_confirm, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.menu_item_confirm:
        Intent intent = new Intent();
        mParams.setStartDate(slStartTime.getTextContent());
        mParams.setEndDate(slEndTime.getTextContent());
        mParams.setOrderNo(slOrderNo.getTextContent());
        intent.putExtra(EXTRA_PARAMS, mParams);
        mActivity.setResult(Activity.RESULT_OK, intent);
        mActivity.finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }
}
